package cn.biplam.front.template.control.tree;

import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.KvMap;
import cn.biplam.front.html.HtmlDiv;
import cn.biplam.front.html.HtmlImage;
import cn.biplam.front.html.base.HtmlWindow;
import cn.biplam.front.html.base.Const.Float_Type;
import cn.biplam.front.html.base.Const.Position_Mode;
import cn.biplam.front.html.base.Const.Visibility_Mode;

/**
 * 功能	    树自定义图标。自定义图片根据树节点的属性来设置，
 *      及同一属性的节点对应同一类型的图标	。
 * @author 	WangSong
 * @create 	2009-4-23 下午04:16:34
 * @version 1.0
 */
public class IconOld {
	
	/**节点图标 */
	private HtmlWindow icon;
	
	/**设置的图标集 */
	private KvMap<String,String> setIcon;
	private String sHomeUrl;
	private boolean isCustom;
	
	/**
	 * @throws ErrorMessage 
	 * 功能：构造方法
	 */
	public IconOld(String sHomeUrl) throws ErrorMessage{
		this.sHomeUrl = sHomeUrl;
		this.setIcon = new KvMap<String,String>();
		this.isCustom = false;
		this.icon = new HtmlDiv();
		this.icon.setCssID("tinorm");
		this.icon.setPositionMode(Position_Mode.Relative);
		this.icon.setFloat(Float_Type.Left);
		/**默认图标集合 */
		this.setIcon.put("leaf",this.sHomeUrl+"/images/,df.gif");
		this.setIcon.put("unleaf",this.sHomeUrl+"/images/,duf.gif");
		this.setIcon.put("rootleaf",this.sHomeUrl+"/images/,odf.gif");
	}
	
	/**
	 * 功能   设置节点图标
	 * @param nodeType  节点的类型
	 * @param sHomeUrl  主应用路径
	 * @param iconName  图片名称及路径(不包含主应用路径)
	 * @throws ErrorMessage 
	 */
	public void setIcon(String nodeType,String iconPath,String iconName) throws ErrorMessage{
		this.setIcon.put(nodeType, sHomeUrl+iconPath+","+iconName);
		this.isCustom = true;
	}
	
	/**
	 * 功能  取节点图标
	 * @param ID    节点图标id (tree_Id+","+node_id)
	 * @param nodeType  节点类型
	 * @return HtmlWindow
	 * @throws ErrorMessage 
	 */
	public HtmlWindow get(String ID,String nodeType,char isLeaf,boolean isRoot) throws ErrorMessage{
		ID = ID+"icon,";
		if(this.isCustom){
			this.getCustom(ID, nodeType, isLeaf,isRoot);
		}else{
			this.getDeault(ID, isLeaf,isRoot);
		}
		return this.icon;
	}
	
	/**
	 * 功能  取自定义节点图标，如果没有设置当前节点的图标，取默认图标
	 * @param ID    节点图标id (tree_Id+","+node_id)
	 * @param nodeType  节点类型
	 * @return HtmlWindow
	 * @throws ErrorMessage 
	 */
	private void getCustom(String ID,String nodeType,char isLeaf,boolean isRoot) throws ErrorMessage{
		if(this.setIcon.getByKey(nodeType)!=null && 
				(this.setIcon.getByKey(nodeType)).split(",").length>1){
			this.getPic(ID,nodeType,isLeaf,isRoot);
		}else{
			this.getDeault(ID, isLeaf, isRoot);
		}
	}
	
	/**
	 * 功能         取默认节点图标
	 * @param ID     节点图标id
	 * @param isLeaf  节点是否是叶子节点
	 * @return
	 * @throws ErrorMessage 
	 */
	private void getDeault(String ID,char isLeaf,boolean isRoot) throws ErrorMessage{
		String nodeType = "leaf";
		if(isLeaf == '1'){
			nodeType = "unleaf";
		}else if(isRoot){
			nodeType = "rootleaf";
		}
		this.getPic(ID,nodeType,isLeaf,isRoot);
	}
	
	private void getPic(String ID,String nodeType,char isLeaf,boolean isRoot) throws ErrorMessage{
		String iconName = this.setIcon.getByKey(nodeType).split(",")[1];
		String iconPath = this.setIcon.getByKey(nodeType).split(",")[0];
		if(isRoot){
			HtmlWindow extend = this._getIcon(ID+"s",iconPath+"o"+iconName);
			extend.setVisibility(Visibility_Mode.Hidden);
			this.icon.put(this._getIcon(ID+"u",iconPath+iconName));
			this.icon.put(extend);
		}else{
			if(isLeaf == '1'){/**不是叶子节点*/
				HtmlWindow open = this._getIcon(ID+"s",iconPath+"o"+iconName);
				HtmlWindow uopen = this._getIcon(ID+"u",iconPath+iconName);
				open.setVisibility(Visibility_Mode.Hidden);
				this.icon.put(open);
				this.icon.put(uopen);
			}else{
				this.icon.put(this._getIcon(ID+"u",iconPath+iconName));
			}
		}
	}
	
	/**
	 * 功能：创建一个用于图片切换的内置Div
	 */
	private HtmlWindow _getIcon(String ID,String imageUrl) throws ErrorMessage{
		HtmlWindow div = new HtmlDiv(ID);
		div.setPositionMode(Position_Mode.Absolute);
		HtmlWindow mImage=new HtmlImage(imageUrl,"tinorm");
		div.put(mImage);
		return div;
	}
	
}
